/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fsquare.controller.catalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.broadleafcommerce.core.catalog.domain.Category;
import org.broadleafcommerce.core.catalog.domain.CategoryXref;
import org.broadleafcommerce.core.catalog.domain.Product;
import org.broadleafcommerce.core.catalog.service.CatalogService;
import org.broadleafcommerce.core.web.controller.catalog.BroadleafCategoryController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fsquare.inventory.domain.SkuMovement;

/**
 * This class works in combination with the CategoryHandlerMapping which finds a category based upon
 * the passed in URL.
 */
@Controller("blCategoryController")
public class CategoryController extends BroadleafCategoryController {
    
	@Resource(name = "blCatalogService")
	protected CatalogService catalogService;
	
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return super.handleRequest(request, response);
    }

    
    
    @RequestMapping(value = "/category/get", produces = "application/json")
    @ResponseBody
    public Map<String, Object> getProductJson(HttpServletRequest request, HttpServletResponse response){
    	Map<String, Object> result = new HashMap<String, Object>();
    	String categoryName = request.getParameter("categoryName");
    	
    	List<String> errors = new ArrayList<String>();
    	if(categoryName == null || categoryName.equals("")){
    		errors.add("Invalid Category Name");
    		result.put("errors", errors);
    		return result;
    	}
    	
    	List<Category> categories = catalogService.findCategoriesByName(categoryName);
//        ndCategoryById(Long.parseLong(categoryId));
    	Category category = categories.get(0);
        List<CategoryXref> childCategories = category.getAllChildCategoryXrefs();
        List<Map<String,Object>> childCategoriesList = new ArrayList<Map<String,Object>>();
        for(CategoryXref categoryXref : childCategories){
        	Map<String,Object> cat = new HashMap<String, Object>();
        	
        	Category child = categoryXref.getSubCategory();
        	
        	cat.put("categoryName", child.getName());
        	cat.put("categoryId", child.getId());
        	cat.put("url", child.getUrl());
        	cat.put("generatedUrl", child.getGeneratedUrl());
        	cat.put("displayOrder", categoryXref.getDisplayOrder());
        	
        	childCategoriesList.add(cat);
        }

        result.put("childCategories", childCategoriesList);
        result.put("categoryName", category.getName());
        result.put("categoryId", category.getId());
        result.put("url", category.getUrl());
        result.put("generatedUrl", category.getGeneratedUrl());
        
        
    	return result;
    }
    
    @RequestMapping(value = "/category/save", produces = "application/json")
    @ResponseBody
    public Map<String, Object> updateInventory(HttpServletRequest request, HttpServletResponse response){
    	Map<String, Object> result = new HashMap<String, Object>();
    	
    	return result;
    }
    
    
    
    
    
	public CatalogService getCatalogService() {
		return catalogService;
	}

	public void setCatalogService(CatalogService catalogService) {
		this.catalogService = catalogService;
	}

}
